package com.gali.mall.controller;

import com.gali.mall.common.Const;
import com.gali.mall.common.ResultInfo;
import com.gali.mall.exception.GaLiException;
import com.gali.mall.exception.GaLiExceptionEnum;
import com.gali.mall.model.pojo.User;
import com.gali.mall.service.UserService;
import com.gali.mall.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
   private UserService userService;
    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     * @throws GaLiException
     */
@PostMapping("/register")
@ResponseBody
public ResultInfo register(@RequestParam("username") String username, @RequestParam("password") String password) throws GaLiException {
    if (StringUtils.isEmpty(username)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_USER_NAME);
    }
    if (StringUtils.isEmpty(password)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_PASSWORD);
    }
    if (password.length()<8||password.length()>16){
        return new ResultInfo().error(GaLiExceptionEnum.PASSWORD_TOO_LONG_OR_SHORT);
    }
    User user=new User();
    user.setUsername(username);
    String md5Pwd= MD5Util.getMD5(password);
    user.setPassword(md5Pwd);
    userService.register(user);
        return ResultInfo.success();
}
/**
 * 用户登录
 */
@PostMapping("/login")
@ResponseBody
public ResultInfo login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session)throws GaLiException{
    if (StringUtils.isEmpty(username)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_USER_NAME);
    }
    if (StringUtils.isEmpty(password)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_PASSWORD);
    }
    User user = userService.login(username,password);
    user.setPassword(null);
    session.setAttribute(Const.USER_NAME,user);
    return ResultInfo.success(user);
}
/**
 * 更新签名
 */
@RequestMapping("/update")
@ResponseBody
public ResultInfo updataSignature(@RequestParam("signaturn") String signaturn,HttpSession session){
    User user=(User)session.getAttribute(Const.USER_NAME);
    if (user==null){
        return new ResultInfo().error(GaLiExceptionEnum.UPDATE_WORN);
    }
    userService.updateToSignature(user.getId(),signaturn);
    return ResultInfo.success();
}
/**
 * 退出登录
 */
@GetMapping("/logout")
@ResponseBody
public ResultInfo logout(HttpSession session){
    User user=(User)session.getAttribute(Const.USER_NAME);
    if (user==null){
        return new ResultInfo().error(GaLiExceptionEnum.LOGOUT_WARN);
    }
    session.removeAttribute(String.valueOf(user));
    return ResultInfo.success();
}
/**
 * 管理员登录
 */
@PostMapping("/admin/login")
@ResponseBody
public ResultInfo adminLogin(@RequestParam("username") String username,@RequestParam("password") String password,HttpSession session) throws GaLiException {
    if (StringUtils.isEmpty(username)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_USER_NAME);
    }
    if (StringUtils.isEmpty(password)){
        return new ResultInfo().error(GaLiExceptionEnum.NEED_PASSWORD);
    }
    User user = userService.adminLogin(username,password);
    session.setAttribute(Const.USER_NAME,user);
    return ResultInfo.success(user);
}
@GetMapping("/findAll")
@ResponseBody
  public ResultInfo findAll(){
      List<User> all = userService.findAll();
      if (all.size()>0){
          return ResultInfo.success(all);
      }
      return ResultInfo.error(GaLiExceptionEnum.SELECT_ERROR);
  }
}
